using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MilageMVC.Models;
using MilageMVC.ViewModel;

namespace MilageMVC.Controllers
{
    public class MileagesController : Controller
    {
        private MilageDB _db = new MilageDB();


        public ActionResult Index([Bind(Prefix = "id")]int VehicleID)
        {

             var vehicle = _db.Vehicles
                                .Include(x => x.Mileages)
                                .Include(x => x.Mileages.Select(y  => y.VolumeUnits)  )
                                .Include(x => x.Mileages.Select(y  => y.DistanceType) )
                                .Where(x => x.VehicleID == VehicleID).FirstOrDefault<Vehicle>();

            //Get the mileage for the vehicle. 
            var vvm =  VehiclesController.CalculateVehicleMileage(vehicle);
            ViewBag.Mileage = vvm.Mileage;
            ViewBag.Units = vvm.Units;
            ViewBag.UKMPG = vvm.MilesPerImperialGallon;
            ViewBag.USMPG = vvm.MilesPerUSGallon;
             
            if (vehicle != null)   
            {
                return View(vehicle);
            }
            else
            {
                return HttpNotFound();
            }
        }


        //
        // GET: /Mileages/Details/5

        public ViewResult Details(int id)
        {
            Mileage mileage = _db.Mileages.Single(x => x.MileageID == id);
            return View(mileage);
        }

        //
        // GET: /Mileages/Create

        public ActionResult Create()
        {
            var model = new Mileage();

            model.DateEntered = DateTime.Now;

            AddVolumeUnitsToViewBag();

            return View(model);
        }

        private void AddVolumeUnitsToViewBag()
        {
            ViewBag.VolumeUnits = _db.VolumeUnits.ToList<VolumeUnit>().AsEnumerable<VolumeUnit>();
            ViewBag.DistanceTypes = _db.DistanceTypes.ToList<DistanceType>().AsEnumerable<DistanceType>();
        }

        //
        // GET: /Mileages/Create

        public ActionResult CreateByVehicle(int Id)
        {
            TempData["VehicleId"] = Id;

            AddVolumeUnitsToViewBag();

            return RedirectToAction("Create");
        }

        //
        // POST: /Mileages/Create

        [HttpPost]
        public ActionResult Create(Mileage mileage)
        {

            mileage.UserName = User.Identity.Name;

            LookupValues(mileage);
            

            if (ModelState.IsValid)
            {
                _db.Mileages.Add(mileage);
                _db.SaveChanges();
                return RedirectToAction("Index", new { id = mileage.VehicleID });
            }
            else
            {
                AddVolumeUnitsToViewBag();
                return View(mileage);
            }

        }

        private void LookupValues(Mileage mileage)
        {
            mileage.VolumeUnits  = _db.VolumeUnits.Find(mileage.VolumeUnits.Id);
            mileage.DistanceType = _db.DistanceTypes.Find(mileage.DistanceType.Id);
        }

        //
        // GET: /Mileages/Edit/5

        [HttpGet]
        public ActionResult Edit(int id)
        {
            Mileage mileage = _db.Mileages.Single(x => x.MileageID == id);

            AddVolumeUnitsToViewBag();

            return View(mileage);
        }

        //
        // POST: /Mileages/Edit/5

        [HttpPost]
        public ActionResult Edit(Mileage mileage)
        {

            LookupValues(mileage);

            if (ModelState.IsValid)
            {
                _db.Entry(mileage).State = EntityState.Modified;
                _db.SaveChanges();
                return RedirectToAction("Index", "Mileages", new { id = mileage.VehicleID });
            }
            return View(mileage);
        }

        //
        // GET: /Mileages/Delete/5

        public ActionResult Delete(int id)
        {
            Mileage mileage = _db.Mileages.Single(x => x.MileageID == id);
            return View(mileage);
        }

        //
        // POST: /Mileages/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Mileage mileage = _db.Mileages.Single(x => x.MileageID == id);
            _db.Mileages.Remove(mileage);
            _db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                _db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}